<?php
/*
 * 广告逻辑处理处理类
 * By:Jin
 * 2017.12.21
**/
namespace app\index\controller;

//代理广告
use app\common\model\Ad;
use think\facade\Session;

class Ads extends Base
{
	//展示广告
    public function ads()
    {
        //分类
        $ty_list = [0=>'所有广告',1=>'在播广告',2=>'已下播广告',3=>'待播广告',4=>'待审核广告',5=>'审核未通过广告'];
        $inc_type = $this->request->param('inc_type',0);//类别键
    	//获取所有广告
        $data['ad_shop_id'] = Session::get('sp_id','index');
        $agem_id= db('shops')->where('sp_id',$data['ad_shop_id'])->find();
        $data['ad_agents_id']=$agem_id['sp_agem_id'];
//        var_dump($data);die;
        if($inc_type!=0){
            $data['ad_status'] = $inc_type;
        }
        $list = Ad::with('shops')->where($data)->order('ad_start_time','desc')->paginate(10,false,[
        'type'     => 'bootstrap',
        ]);
//       dump($list);die;
        $page = $list->render();
        $this->assign('page',$page);
        $this->assign('inc_type',$inc_type);
        $this->assign('ty_list',$ty_list);
        $this->assign('list', $list);
//        dump($list);die;
        return $this->fetch();
    }

    //下播
    public function stop()
    {
        $res = $this->request->get();
        // 1正在播放2已播出,3已审核未播,4未审核未播,5未通过审核
        switch($res['info']){
            case 1:
                $status = 2;
                break;
            case 2:
                $status = 3;
                break;
            case 3:
                $status = 5;
                break;
            default:
                $status = 2;
                break;
        }

        $output = Ad::where('ad_id',$res['id'])->update(['ad_status'=>$status]);
        if($output){
            return $this->success('修改成功！');
        }else{
            return $this->error('修改失败！');
        }
    }
    /**
     * 商户添加广告
     */
    public function add_ads(){
        $request=$this->request;
        $ad_mart=$request->param('ad_mart');
//        var_dump($ad_mart);die;
        if($ad_mart==1){
            $this->assign('ad_mart',$ad_mart);
            return $this->fetch('add_ads');

        }else{
            $this->assign('ad_mart',$ad_mart);
            return $this->fetch('add_ads2');
        }

    }
    /**
     * 商户添加广告数据处理
     */
    public function  add_act(){
        $request=$this->request;
        $data=$request->param();
//        dump($data);die;
        $data['ad_shop_id']=Session::get('sp_id','index');
        $agem_id= db('shops')->where('sp_id',$data['ad_shop_id'])->find();
        $data['ad_agents_id']=$agem_id['sp_agem_id'];
        $data['ad_status']=4;
        $data['ad_start_time']=strtotime($data['ad_start_time']);
        $data['ad_end_time']=strtotime($data['ad_end_time']);
//        var_dump($data);die;

        $model=new Ad();
        if($data['ad_mart']==1){
            $res=$model->data($data)->save();
            if(!$res){
                $this->error('申请失败，请重试','index/ads');
            }
            $this->success('申请成功，请耐心等待','ads/ads');
        }elseif ($data['ad_mart']==2){
            $file=request()->file('ad_image_url');

//            var_dump($file->info);die;

            $fileinfo = $file->validate(['size' => 2097152, 'ext' => 'jpg,png,jpeg'])->move('public/upload/ad');
            if($fileinfo){
                $url=$fileinfo->getSaveName();
//                dump($url);die;
                $data['ad_image_url']="public/upload/ad/".$url;
                $res=$model->data($data)->save();
                if($res){
                    $this->success('申请成功，请耐心等待','ads/ads');
                }else{

                    $this->error('申请失败，请重试','ads/ads');
                }
            }else{
                $this->error('申请失败，请重试','ads/ads');
            }

        }
    }

}